home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet multimedia / Animacje, filmy i prezentacje / Odtwarzacze filmow / JahPlayer 0.1.0 / jahplayer-0.1.0-installer.exe / openlibraries-0.3.0-runtime.exe / shaders / fixed_function.vert next >
Text File  |  2006-03-27  |  5KB  |  183 lines

  1.  
  2. // fixed_function.vert - A fixed function emulator vertex shader to sg.
  3.  
  4. // Copyright (C) 2005-2006 Visual Media FX Ltd.
  5. // Released under the LGPL.
  6. // For more information, see http://www.openlibraries.org.
  7.  
  8. // Modified from the automatically generated GLSL ShaderGen from 3Dlabs.
  9.  
  10. vec4 Ambient;
  11. vec4 Diffuse;
  12. vec4 Specular;
  13.  
  14. void pointLight( in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3 )
  15. {
  16.     float nDotVP;       // normal . light direction
  17.     float nDotHV;       // normal . light half vector
  18.     float pf;           // power factor
  19.     float attenuation;  // computed attenuation factor
  20.     float d;            // distance from surface to light source
  21.     vec3  VP;           // direction from surface to light position
  22.     vec3  halfVector;   // direction of maximum highlights
  23.  
  24.     // Compute vector from surface to light position
  25.     VP = vec3( gl_LightSource[ i ].position ) - ecPosition3;
  26.  
  27.     // Compute distance between surface and light position
  28.     d = length( VP );
  29.  
  30.     // Normalize the vector from surface to light position
  31.     VP = normalize( VP );
  32.  
  33.     // Compute attenuation
  34.     attenuation = 1.0 / ( gl_LightSource[ i ].constantAttenuation + gl_LightSource[ i ].linearAttenuation * d + gl_LightSource[ i ].quadraticAttenuation * d * d );
  35.  
  36.     halfVector = normalize( VP + eye );
  37.  
  38.     nDotVP = max( 0.0, dot( normal, VP ) );
  39.     nDotHV = max( 0.0, dot( normal, halfVector ) );
  40.  
  41.     if( nDotVP == 0.0 )
  42.     {
  43.         pf = 0.0;
  44.     }
  45.     else
  46.     {
  47.         pf = pow( nDotHV, gl_FrontMaterial.shininess );
  48.     }
  49.  
  50.     Ambient  += gl_LightSource[ i ].ambient * attenuation;
  51.     Diffuse  += gl_LightSource[ i ].diffuse * nDotVP * attenuation;
  52.     Specular += gl_LightSource[ i ].specular * pf * attenuation;
  53. }
  54.  
  55. void spotLight( in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3 )
  56. {
  57.     float nDotVP;            // normal . light direction
  58.     float nDotHV;            // normal . light half vector
  59.     float pf;                // power factor
  60.     float spotDot;           // cosine of angle between spotlight
  61.     float spotAttenuation;   // spotlight attenuation factor
  62.     float attenuation;       // computed attenuation factor
  63.     float d;                 // distance from surface to light source
  64.     vec3  VP;                // direction from surface to light position
  65.     vec3  halfVector;        // direction of maximum highlights
  66.  
  67.     // Compute vector from surface to light position
  68.     VP = vec3( gl_LightSource[ i ].position ) - ecPosition3;
  69.  
  70.     // Compute distance between surface and light position
  71.     d = length( VP );
  72.  
  73.     // Normalize the vector from surface to light position
  74.     VP = normalize( VP );
  75.  
  76.     // Compute attenuation
  77.     attenuation = 1.0 / ( gl_LightSource[ i ].constantAttenuation + gl_LightSource[ i ].linearAttenuation * d + gl_LightSource[ i ].quadraticAttenuation * d * d );
  78.  
  79.     // See if point on surface is inside cone of illumination
  80.     spotDot = dot( -VP, normalize( gl_LightSource[ i ].spotDirection ) );
  81.  
  82.     if( spotDot < gl_LightSource[ i ].spotCosCutoff )
  83.     {
  84.         spotAttenuation = 0.0; // light adds no contribution
  85.     }
  86.     else
  87.     {
  88.         spotAttenuation = pow( spotDot, gl_LightSource[ i ].spotExponent );
  89.     }
  90.  
  91.     // Combine the spotlight and distance attenuation.
  92.     attenuation *= spotAttenuation;
  93.  
  94.     halfVector = normalize( VP + eye );
  95.  
  96.     nDotVP = max( 0.0, dot( normal, VP ) );
  97.     nDotHV = max( 0.0, dot( normal, halfVector ) );
  98.  
  99.     if( nDotVP == 0.0 )
  100.     {
  101.         pf = 0.0;
  102.     }
  103.     else
  104.     {    
  105.         pf = pow( nDotHV, gl_FrontMaterial.shininess );
  106.     }
  107.  
  108.     Ambient  += gl_LightSource[ i ].ambient * attenuation;
  109.     Diffuse  += gl_LightSource[ i ].diffuse * nDotVP * attenuation;
  110.     Specular += gl_LightSource[ i ].specular * pf * attenuation;
  111. }
  112.  
  113. void directionalLight( in int i, in vec3 normal )
  114. {
  115.     float nDotVP; // normal . light direction
  116.     float nDotHV; // normal . light half vector
  117.     float pf;     // power factor
  118.  
  119.     nDotVP = max( 0.0, dot( normal, normalize( vec3( gl_LightSource[ i ].position ) ) ) );
  120.     nDotHV = max( 0.0, dot( normal, vec3( gl_LightSource[ i ].halfVector ) ) );
  121.  
  122.     if( nDotVP == 0.0 )
  123.     {
  124.         pf = 0.0;
  125.     }
  126.     else
  127.     {
  128.         pf = pow( nDotHV, gl_FrontMaterial.shininess );
  129.     }
  130.  
  131.     Ambient  += gl_LightSource[ i ].ambient;
  132.     Diffuse  += gl_LightSource[ i ].diffuse * nDotVP;
  133.     Specular += gl_LightSource[ i ].specular * pf;
  134. }
  135.  
  136. vec3 fnormal( void )
  137. {
  138.     //Compute the normal 
  139.     vec3 normal = gl_NormalMatrix * gl_Normal;
  140.     normal = normalize( normal );
  141.     return normal;
  142. }
  143.  
  144. void flight( in vec3 normal, in vec4 ecPosition, float alphaFade )
  145. {
  146.     vec4 color;
  147.     vec3 ecPosition3;
  148.     vec3 eye;
  149.  
  150.     ecPosition3 = ( vec3( ecPosition ) ) / ecPosition.w;
  151.     eye = vec3( 0.0, 0.0, 1.0 );
  152.  
  153.     // Clear the light intensity accumulators
  154.     Ambient  = vec4( 0.0 );
  155.     Diffuse  = vec4( 0.0 );
  156.     Specular = vec4( 0.0 );
  157.  
  158.     pointLight( 0, normal, eye, ecPosition3 );
  159.  
  160.     color = gl_FrontLightModelProduct.sceneColor + Ambient  * gl_FrontMaterial.ambient + Diffuse  * gl_FrontMaterial.diffuse;
  161.     color += Specular * gl_FrontMaterial.specular;
  162.     color = clamp( color, 0.0, 1.0 );
  163.     gl_FrontColor = color;
  164.  
  165.     gl_FrontColor.a *= alphaFade;
  166. }
  167.  
  168. void main( void )
  169. {
  170.     vec3  transformedNormal;
  171.     float alphaFade = 1.0;
  172.  
  173.     // Eye-coordinate position of vertex, needed in various calculations
  174.     vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
  175.  
  176.     // Do fixed functionality vertex transform
  177.     gl_Position = ftransform( );
  178.     transformedNormal = fnormal( );
  179.     flight( transformedNormal, ecPosition, alphaFade );
  180.     
  181.     gl_TexCoord[ 0 ] = gl_MultiTexCoord0;
  182. }
  183.